﻿using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using ManagementTool.ESB;
using SpringEngine;
using Point = SpringEngine.Point;

namespace ManagementTool
{
    public partial class ServiceUserControl : UserControl, ISpringable
    {
        private readonly ExposeServiceBusClient serviceBusClient;
        public ESB.Service Service { get; private set; }
        public string Text
        {
            get
            {
                string text = Service.Name;
                if (text == "IRequestHandler")
                    return "DataAccess";
                if (text.StartsWith("I"))
                    text = text.Substring(1);

                var characters = text.ToCharArray();
                string name ="";
                foreach (char c in characters)
                {
                    if (Char.IsUpper(c))
                        name += ' ';
                    name += c;
                }
                return name;
            }
        }

        public ServiceUserControl(ESB.Service service, ESB.ExposeServiceBusClient serviceBusClient)
        {
            this.serviceBusClient = serviceBusClient;
            Service = service;
            // Required to initialize variables
            InitializeComponent();
            this.uxText.Text = Text;

        }

        public Point GetPosition()
        {
            return new Point(Canvas.GetLeft(this)+Width/2, Canvas.GetTop(this)+Height/2);
        }

        public void SetPosition(Point position)
        {
            Canvas.SetLeft(this, position.X-Width/2);
            Canvas.SetTop(this, position.Y-Height/2);
        }


        private void Ellipse_MouseEnter(object sender, MouseEventArgs e)
        {
            var serviceInfoWindow = new ServiceInfoWindow(Service, serviceBusClient);
            serviceInfoWindow.Show();

        }

    }
}